﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;

namespace map.PayEase
{
    public partial class DelayResult : System.Web.UI.Page
    {
        public string mm;
        public string md;
        public string md5money;
        public string mac;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string errorMsg =  InitPayEase();
                if (errorMsg!=string.Empty )
                {
                    Response.Write("error");
                }
                else
                {
                    Response.Write("sent");
                }

            }
        }


        #region 辅助方法
        private string  InitPayEase()
        {
            string errorMsg = string.Empty;

            string v_count = Request.QueryString.Get("v_count");
            int count =0;
            try
            {
                count =Convert.ToInt32 (v_count );
            }
            catch (Exception e1)
            {
                errorMsg +="返回结果数目格式不正确"+v_count +";";
            }

            string v_oid = Request.QueryString.Get("v_oid");
            string[] _oids = resolveQueryValue(v_oid, "|");

            string v_pmode = Request.QueryString.Get("v_pmode");
            string[] _pmodes = resolveQueryValue(v_pmode, "|");

            string v_pstatus = Request.QueryString.Get("v_pstatus");
            string[] _pstatuss = resolveQueryValue(v_pstatus ,"|");

            string v_pstring = Request.QueryString.Get("v_pstring");
            string[] _pstrings = resolveQueryValue(v_pstatus, "|");

            string v_amount = Request.QueryString.Get("v_amount");
            string[] _amounts = resolveQueryValue(v_pstatus, "|");

            string v_moneytype = Request.QueryString.Get("v_moneytype");
            string[] _moneytypes = resolveQueryValue(v_pstatus, "|");

            bool countError = false;
            if (_amounts.Length != count || _moneytypes.Length != count || _oids.Length != count
                || _pmodes.Length != count || _pstatuss.Length != count || _pstrings.Length != count)
            {
                errorMsg += "返回结果参数数目不匹配;";
                countError = true;
            }

            string v_md5money = Request.QueryString.Get("v_md5money");
            string v_mac = Request.QueryString.Get("v_mac");
            string v_sign = Request.QueryString.Get("v_sign");

            //MD5指纹验证
            ClassHmac2003.ClassHmac cc = new ClassHmac2003.ClassHmac();
            mm = v_oid + v_pmode + v_pstatus + v_pstring + v_count;
            mac = cc.hex_hmac_md5(mm, PayEaseUtil.md5_key);


            md = v_amount + v_moneytype;
            md5money = cc.hex_hmac_md5(md, PayEaseUtil.md5_key);

            if (mac != v_mac)
            {
                errorMsg += "mac验证不正确;";
            }
            if (md5money != v_md5money)
            {
                errorMsg += "md5money验证不正确;";
            }

            

            //RSA验证
            String strSource = v_oid + v_pstatus + v_amount + v_moneytype + v_count;

            RSA_MD5 rsa = new RSA_MD5();
            int d = rsa.PublicVerifyMD5(Server.MapPath("Public1024.key"), v_sign, strSource);
            Response.Write(d);

            if (d != 0)
            {
                errorMsg += "RSA验证不正确;";
            }


            cbPayEaseReturnTrans returnTrans = cbPayEaseReturnTrans.Find("v_oid like '%"+v_oid +"%'");
            if (returnTrans.Count <= 0)                   //判断是否已经处理过了
            {
                try
                {
                    string queryStr = Request.QueryString.ToString();
                    returnTrans.New();
                    returnTrans.v_amount.Value = v_amount;
                    returnTrans.v_count.Value = count;
                    returnTrans.v_mac.Value = v_mac;
                    returnTrans.v_md5money.Value = v_md5money;
                    returnTrans.v_moneytype.Value = v_moneytype;
                    returnTrans.v_oid.Value = v_oid;
                    returnTrans.v_pmode.Value = v_pmode;
                    returnTrans.v_pstatus.Value = v_pstatus;
                    returnTrans.v_pstring.Value = v_pstring;
                    returnTrans.v_sign.Value = v_sign;
                    returnTrans.v_md5money.Value = v_md5money;
                    returnTrans.ReturnType.Value = RuleCode.Enum.PayEaseTranType.RelayReturn;
                    returnTrans.OriginResult.Value = queryStr;
                    returnTrans.FinalResult.Value = errorMsg;
                    returnTrans.Insert();

                    returnTrans.CurrentIndex = 0;


                    if (countError == false && errorMsg ==string.Empty )  //判断指纹验证
                    {
                        for (int i = 0; i < count; i++)
                        {
                            if (_pstatuss[i] == "1")  //判断状态
                            {
                                cbuser user = cbuser.Find("recid in (select v_ordername as recid from dbo.cbPayEaseOrderTrans where v_oid='" + _oids[i] + "')");
                                if (user.Count > 0 )
                                {
                                    double amount = 0.0;
                                    try
                                    {
                                        amount = Convert.ToDouble(_amounts[i]);
                                    }
                                    catch { }

                                    DateTime curDate = DateTime.Now;
                                    cbmoneytrans moneytran = cbmoneytrans.Find("RefRecID='" + _oids[i] + "'");
                                    if (moneytran.Count <= 0)
                                    {
                                        //moneytran.New();
                                        //moneytran.cbmoney.Value = -amount;
                                        //moneytran.cbmoneytrantype.Value = MONEY_TransType.BankOut;
                                        //moneytran.cbuserid.Value = user.cbuserid.Value;
                                        //moneytran.jccreatedate.Value = curDate;
                                        //moneytran.jccreatetime.Value = (int)(curDate.TimeOfDay.TotalSeconds);
                                        //moneytran.jcdesc.Value = queryStr;
                                        //moneytran.NoYes_dongjie.Value = RuleCode.Enum.NoYes.No;
                                        //moneytran.RefRecID.Value = _oids[i];
                                        //moneytran.RefTable.Value = "cbPayEaseReturnTrans";
                                        //moneytran.Insert();

                                        moneytran.New();
                                        moneytran.cbmoney.Value = amount;
                                        moneytran.cbmoneytrantype.Value = MONEY_TransType.ChargeIn;
                                        moneytran.cbuserid.Value = user.cbuserid.Value;
                                        moneytran.jccreatedate.Value = curDate;
                                        moneytran.jccreatetime.StringValue = curDate.ToLongTimeString ();
                                        moneytran.jcdesc.Value = queryStr;
                                        moneytran.NoYes_dongjie.Value = RuleCode.Enum.NoYes.No;
                                        moneytran.RefRecID.Value = _oids[i];
                                        moneytran.RefTable.Value = "cbPayEaseReturnTrans";
                                        moneytran.Insert();


                                        user.cbmoney.Value += amount;
                                        user.cbmoneyZ.Value += amount;
                                        user.Modify();
                                    }
                                    else
                                    {
                                        errorMsg += "该订单已经充值成功,不能继续使用;";
                                    }
                                }    
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                }
            }
            else
            {
                errorMsg += "该订单日志已经记录过; ";
            }
            return errorMsg;
        }

        
        private string[] resolveQueryValue(string queryValue, string splitKey)
        {
            return queryValue.Trim ().Split(splitKey.ToCharArray());
        }
        #endregion
    }
}
